export class Decrypter {
    static headerlength=16;
    static encryptionKey = null;

    static extMap={
        ogg:'rpgmvo',
        m4a:'rpgmvm',
        png:'rpgmvp'
    }

    /**
     * 
     * @param {ArrayBuffer} input 
     * @returns {ArrayBuffer}
     */
    static decryptArrayBuffer(input) {
        if (!input) return null;
        
        const out = input.slice(this.headerlength);
        if (out) {
            const byteArray = new Uint8Array(out);
            for (let i = 0; i < this.headerlength; i++) {
                byteArray[i] ^= this.encryptionKey[i];
            }
        }

        return out;
    }

    static readEncryptionkey() {
        if(!this.encryptionKey){
            let keysString = $dataSystem.encryptionKey.split(/(.{2})/).filter(Boolean);
            this.encryptionKey=Uint8Array.from(keysString.map(value=>parseInt(value,16)));
        }
    }

    static extToEncryptExt(url) {
        const ext = url.slice(-3);
        let encryptedExt = this.extMap[ext];
        if(encryptedExt)
            return `${url.slice(0, -3)}${encryptedExt}`;
        return url;
    }
}